import { controller, get , log} from '../decorator/router'
import mongoose from 'mongoose'

const areaDataBase = mongoose.model('area')
const areaHouseDataBase = mongoose.model('areaHouse')
const detailHouse = mongoose.model('detailHouse')

@controller('')
export class Crawler{
    /**
     * 获取杭州城区下的房子信息
     */
    @get('/getDetail')
    @log
    async detailHouse (ctx,next){
        let query = ctx.query
        let { _id } = query;
        if (!_id) return (ctx.body = '_id is required')

        let area = await detailHouse
        .findById(_id)
        .exec()
        ctx.body = {
            code:0,
            area
        }
    }
/**
 * 获取杭州城区下的房子信息
 */
    @get('/getAreaHouse')
    @log
    async areaHouse (ctx,next){
        let areaHouse = await areaHouseDataBase
        .find({})
        .exec()
        ctx.body = {
            code:0,
            areaHouse
        }
    }
/**
 * 获取杭州城区单条的名称
 */
    @get('/getArea/:_id')
    @log
    async getArea (ctx,next){
        const { params } = ctx
        const { _id } = params

        if (!_id) return (ctx.body = '_id is required')

        let area = await areaDataBase
        .findById(_id)
        .exec()
        ctx.body = area
    }
/**
 * 获取杭州城区的名称
 */
    @get('/getArea')
    @log
    async Area (ctx,next){
        let area = await areaDataBase
        .find({})
        .exec()
        ctx.body = {
            code:0,
            area
        }
    }

}
